import {computed, reactive, ref} from "vue";
import {useStore} from "vuex";
import {UserEnrollListDAT, UserEnrollZLListDAT} from "../../types/responseTypes";
import {getUserEnrollList} from "../../api/enroll";

export function useUserEnrollList(){

    const enrollListWrap = reactive<{
        enrollList: UserEnrollListDAT[],
        ZLList: UserEnrollZLListDAT[]
    }>({
        enrollList: [],
        ZLList: []
    })
    const {state} = useStore()

    function initUserEnrollList(){
        return new Promise((resolve, reject) => {
            getUserEnrollList(state.user.userInfo.CUSER_ID).then(res => {
                if (res.ERROR[0].errcode == '0'){
                    enrollListWrap.enrollList = JSON.parse(JSON.stringify(res.DAT))
                    enrollListWrap.ZLList = JSON.parse(JSON.stringify(res.ZL))
                    resolve(null)
                }else {
                    uni.showToast({
                        title: res.ERROR[0].error,
                        icon: 'none',
                        mask: true
                    })
                    reject('err')
                }
            }).catch(err => {
                uni.showToast({
                    title: '获取列表失败',
                    icon: 'error',
                    mask: true
                })
                reject('err')
            })
        })
    }

    return {
        enrollList: computed(() => enrollListWrap.enrollList),
        ZLList: computed(() => enrollListWrap.ZLList),
        initUserEnrollList
    }
}
